
/**  
 * @Title: RedisConfig.java  
 * @Package org.sapling.data.config  
 * @Description: TODO
 * @author DcfZero  
 * @date 2021年6月13日  
 * @version V1.0  
 */ 
package org.sapling.data.config;


import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;


/**  
 * TODO
 * @ClassName: RedisConfig  
 * @author DcfZero 
 * @date 2021年6月13日  
 * 
 */
@Configuration
@ConditionalOnProperty(name = {"system.enabled-redis-cache"}, havingValue = "true")
public class RedisConfig{
	private static final Logger log = org.slf4j.LoggerFactory.getLogger(RedisConfig.class);

	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.port}")
	private int port;
	@Value("${spring.redis.timeout}")
	private int timeout;
	@Value("${spring.redis.password}")
	private String password;
	@Value("${spring.redis.pool.max-active}")
	private int maxActive;
	@Value("${spring.redis.pool.max-wait}")
	private int maxWait;
	@Value("${spring.redis.pool.max-idle}")
	private int maxIdle;
	@Value("${spring.redis.pool.min-idle}")
	private int minIdle;   

	@Bean
	public RedisTemplate<String, String> redisTemplate(LettuceConnectionFactory redisConnectionFactory){
		log.info("Redis connection info: {host:{},port:{},timeout:{}}", new Object[] { host,port,timeout});
		RedisTemplate<String, String> template = new RedisTemplate();
		setSerializer(template);
		template.setConnectionFactory((RedisConnectionFactory)redisConnectionFactory);
		template.afterPropertiesSet();
		return template;
	}

	private void setSerializer(RedisTemplate template) {
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		template.setKeySerializer((RedisSerializer)new StringRedisSerializer());
	    template.setHashKeySerializer((RedisSerializer)new StringRedisSerializer());
	    template.setValueSerializer((RedisSerializer)jackson2JsonRedisSerializer);
	    template.setHashValueSerializer((RedisSerializer)jackson2JsonRedisSerializer);
	}
}
